package net.tngou.iask.dao;

import java.util.ArrayList;
import java.util.List;

import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.jdbc.QueryHelper;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Survey;
import net.tngou.iask.util.StringUtil;

/**
 * 问卷
 * @author tngou@tngou.net
 *
 */
public class SurveyDao extends BaseDao {

	/**
	 * 取得问卷
	 * @param page 当前页
	 * @param size 页大小
	 * @param main 实体（）
	 * @param type 类型
	 * @param keyword 搜索关键词
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main,long type,String keyword) {
		List<Survey> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT *";
		String sql_t="SELECT COUNT(id) ";
		String sql_o=" ORDER BY id DESC";//排序
		String sql=" FROM `"+POJO.DABASE+"_survey`";
		if(main!=0)sql=sql+ " WHERE  main IN(0,?) AND type=?";
		else sql=sql+ " WHERE  main=? AND type=?";
		if(StringUtil.isNotEmpty(keyword))
		{
			sql=sql+" AND name LIKE ? ";
			
			Object[] params={main,type,"%"+keyword+"%"};
			list=QueryHelper.query_slice(Survey.class, sql_r+sql+sql_o, page, size, params);			
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}else{
			Object[] params={main,type};
			list=QueryHelper.query_slice(Survey.class, sql_r+sql+sql_o, page, size, params);	
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}		
		return new PageUtil(list, page, size, total);
		
	}
	
	/**
	 * 取得问卷列表
	 * @param main
	 * @param type
	 * @return
	 */
	public List<Survey> getList(long main,long type) {
		String sql_r="SELECT *";
		String sql_o=" ORDER BY id DESC";//排序
		String sql=" FROM `"+POJO.DABASE+"_survey`"
		+ " WHERE  main IN(0,?) AND type=?";
		return QueryHelper.query(Survey.class, sql_r+sql+sql_o, main,type);
	}
	/**
	 * 更新问卷关联库
	 * @param survey 问卷ID
	 * @param diseases 疾病
	 * @param questions 问题
	 */
	@Deprecated
	public void update(long survey,Long[] diseases,Long[] questions) {
		//问题对应相信
		
		if(diseases!=null)
		{
			String delete_sql="DELETE FROM `"+POJO.DABASE+"_survey_disease` WHERE survey=?";
			String add_sql ="INSERT INTO `"+POJO.DABASE+"_survey_disease`( `survey`, `disease`) VALUES (?,?)";
			QueryHelper.update(delete_sql, survey);
			Object[][] params =new Object[diseases.length][2];
			for (int i = 0; i < diseases.length; i++) {
				params[i][0]=survey;
				params[i][1]=diseases[i];
			}
			QueryHelper.batch(add_sql, params );
		}
		
		
		if(questions!=null)
		{
			//问题 对应过滤条件
			String delete_sql="DELETE FROM `"+POJO.DABASE+"_survey_question` WHERE survey=?";
			String add_sql ="INSERT INTO `"+POJO.DABASE+"_survey_question`( `survey`,`question`, `seq`) VALUES (?,?,?)";
			QueryHelper.update(delete_sql, survey);
			Object[][] params =new Object[questions.length][3];
			for (int i = 0; i < questions.length; i++) {
				params[i][0]=survey;
				params[i][1]=questions[i];
				params[i][2]=i+1;
			}
			QueryHelper.batch(add_sql, params );
		}		
	}
	
	/**
	 * 取得疾病对于的案例
	 * @param disease
	 * @return
	 */
	public List<Survey> getSurvey(long disease) {
		String sql="SELECT survey.*  FROM `"+POJO.DABASE+"_survey` survey,`"+POJO.DABASE+"_survey_disease` survey_disease WHERE survey.id=survey_disease.survey AND survey_disease.disease=? ORDER BY id ASC";
		return QueryHelper.query(Survey.class, sql, disease);	
	}
	
	
	
}
